package org.zlpt.contract.dao;

import com.owlike.genson.Genson;
import org.hyperledger.fabric.contract.Context;
import org.hyperledger.fabric.contract.ContractInterface;
import org.hyperledger.fabric.contract.annotation.Contract;
import org.hyperledger.fabric.contract.annotation.Transaction;
import org.hyperledger.fabric.shim.ChaincodeException;
import org.hyperledger.fabric.shim.ChaincodeStub;
import org.zlpt.entity.co.SupplementaryArgeementCO;


@Contract(
        name = "supplementaryArgeement"
)
public final class SupplementaryArgeementContract implements ContractInterface {
    private final Genson genson = new Genson();

    @Transaction(intent = Transaction.TYPE.EVALUATE)
    public boolean SupplementaryArgeementExists(final Context ctx, String supplementaryArgeementJson){
        ChaincodeStub stub = ctx.getStub();
        SupplementaryArgeementCO supplementaryArgeement = genson.deserialize(supplementaryArgeementJson, SupplementaryArgeementCO.class);
        String supplementaryArgeementJSON = stub.getStringState(supplementaryArgeement.formKey());

        return (supplementaryArgeementJSON != null && !supplementaryArgeementJSON.isEmpty());
    }

    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public SupplementaryArgeementCO CreateSupplementaryArgeement(final Context ctx, String supplementaryArgeementJson) {
        ChaincodeStub stub = ctx.getStub();
        if (SupplementaryArgeementExists(ctx, supplementaryArgeementJson)) {
            String errorMessage = String.format("SupplementaryArgeement already exists");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }
        SupplementaryArgeementCO supplementaryArgeement = genson.deserialize(supplementaryArgeementJson, SupplementaryArgeementCO.class);
        stub.putStringState(supplementaryArgeement.formKey(), supplementaryArgeementJson);
        return supplementaryArgeement;
    }
    @Transaction(intent = Transaction.TYPE.EVALUATE)
    public SupplementaryArgeementCO ReadSupplementaryArgeement(final Context ctx, String supplementaryArgeementId) {
        ChaincodeStub stub = ctx.getStub();
        String keyPrefiexSupplementaryArgeementId = SupplementaryArgeementCO.formKey(Long.valueOf(supplementaryArgeementId));
        String supplementaryArgeementJSON = stub.getStringState(keyPrefiexSupplementaryArgeementId);
        if (supplementaryArgeementJSON == null || supplementaryArgeementJSON.isEmpty()) {
            String errorMessage = String.format("SupplementaryArgeement does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }

        SupplementaryArgeementCO supplementaryArgeement = genson.deserialize(supplementaryArgeementJSON, SupplementaryArgeementCO.class);
        return supplementaryArgeement;
    }
    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public SupplementaryArgeementCO UpdateSupplementaryArgeement(final Context ctx, String supplementaryArgeementJson) {
        ChaincodeStub stub = ctx.getStub();
        SupplementaryArgeementCO supplementaryArgeement = genson.deserialize(supplementaryArgeementJson, SupplementaryArgeementCO.class);

        if (!SupplementaryArgeementExists(ctx, supplementaryArgeementJson)) {
            String errorMessage = String.format("SupplementaryArgeement does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }
        String oldSupplementaryArgeementJson = stub.getStringState(supplementaryArgeement.formKey());
        SupplementaryArgeementCO oldSupplementaryArgeement = genson.deserialize(oldSupplementaryArgeementJson, SupplementaryArgeementCO.class);
        String sortedJson = genson.serialize(supplementaryArgeement);
        stub.putStringState(supplementaryArgeement.formKey(), sortedJson);
        return oldSupplementaryArgeement;
    }
    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public void DeleteSupplementaryArgeement(final Context ctx, String supplementaryArgeementId) {
        ChaincodeStub stub = ctx.getStub();

        if (!SupplementaryArgeementExists(ctx, supplementaryArgeementId)) {
            String errorMessage = String.format("SupplementaryArgeement does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }

        stub.delState(supplementaryArgeementId);
    }
}






